package com.mid;

/**
 * Created by Lxk on 2020/2/1.
 */
public class Solution80 {

    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int cur = nums[0];
        //统计当前数字出现次数
        int count = 1;
        int length = nums.length;
        for (int i = 1; i < length; i++) {
            if (nums[i] == cur) {
                count++;
            } else {
                if (count >= 3) {
                    //判定已经出现两次，本次为第一次找到其后不同数值，需要进行迁移
                    for (int k = i; k < length; k++) {
                        nums[k + 2 - count] = nums[k];
                    }
                    length -= (count - 2);
                    cur = nums[i + 2 - count];
                    i -= (count - 2);
                } else {
                    cur = nums[i];
                }
                count = 1;
            }
        }
        if (count >= 3) {
            length -= (count - 2);
        }
        return length;
    }

    public static void main(String[] args) {
        int[] nums = {1, 1, 1};
        Solution80 solution80 = new Solution80();
        System.out.println(solution80.removeDuplicates(nums));
    }

}
